# author:欧阳亨杰
# datetime:2022/5/2 1:38
import unittest

import ddt
from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import pandas as pd


def readFile():
    file = pd.read_csv('../data/comment.csv')
    df = pd.DataFrame(file).fillna('')
    data = df.to_dict(orient='records')
    return data


@ddt.ddt
class reply_test(unittest.TestCase):
    def setUp(self) -> None:
        option = webdriver.ChromeOptions()
        option.binary_location = r'E:\Program Files\Google\Chrome\Application\chrome.exe'
        self.driver = webdriver.Chrome(options=option)
        self.url = 'http://localhost:3000'
        self.driver.implicitly_wait(10)
        self.driver.get(self.url)
        self.driver.maximize_window()
        self.login()

    def login(self):
        username = self.driver.find_element(By.CSS_SELECTOR,
                                            '.el-form > div:nth-child(1) > div > div > input:nth-child(2)')
        password = self.driver.find_element(By.CSS_SELECTOR,
                                            '.el-form > div:nth-child(2) > div > div > input:nth-child(2)')
        username.send_keys('admin')
        password.send_keys('123456')
        btn = self.driver.find_element(By.CSS_SELECTOR, '.el-form > div:nth-child(3) > button')
        btn.click()
        time.sleep(2)
        self.driver.find_element(By.CSS_SELECTOR, '.sidebar > ul > li:nth-child(5)').click()

    @ddt.data(*readFile())
    def test_comment_select(self, data):
        text = self.driver.find_element(By.CSS_SELECTOR, '.container > div:nth-child(1) > div:nth-child(1) > input')
        btn = self.driver.find_element(By.CSS_SELECTOR, '.container > div:nth-child(1) > button:nth-child(2)')
        time.sleep(2)
        text.send_keys(data.get('name'))
        btn.click()
        time.sleep(3)
        num = self.driver.find_elements(By.CSS_SELECTOR, '.el-table__row')
        empty = self.driver.find_elements(By.CSS_SELECTOR, '.el-table__empty-text')
        if data.get('state') == 1:
            # 成功的测试用例是出现下面两者中的任何一个
            self.assertNotEqual(len(num) + len(empty), 0)
        else:
            self.assertEqual(len(num) + len(empty), 0)

    def test_comment_delete(self):
        num1 = self.driver.find_elements(By.CSS_SELECTOR, '.el-table__row')
        btn = self.driver.find_element(By.CSS_SELECTOR,
                                       'tbody > tr:nth-child(4) > td:nth-child(5) > div > button:nth-child(2)')
        btn.click()
        time.sleep(3)
        self.driver.find_element(By.CSS_SELECTOR, '.el-message-box__btns > button:nth-child(2)').click()
        time.sleep(3)
        num2 = self.driver.find_elements(By.CSS_SELECTOR, '.el-table__row')

        self.assertEqual(len(num1) - 1, len(num2))


if __name__ == '__main__':
    unittest.main()
